package com.example.administrator.facetest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

public class MyImage extends ImageView {
    Path path = new Path();
    Paint paint = new Paint();

    public MyImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(path, paint);
    }

    public void showFace(Location location, int rWidth, int rHeight) {
        double left = location.getLeft();
        double top = location.getTop();
        int width = location.getWidth();
        int height = location.getHeight();
        int rotation = location.getRotation();
        double angle = rotation * Math.PI/180;
        double compareW = rWidth/500;
        double compareH = rHeight/300;

        /*Point p1 = new Point((int)(left*compareW), (int)(top*compareH));
        Point p2 = new Point((int)((left + width*Math.cos(angle))*compareW), (int)((top + width*Math.sin(angle))*compareH));
        Point p3 = new Point((int)((left - height*Math.sin(angle))*compareW), (int)((top + height*Math.cos(angle))*compareH));
        Point p4 = new Point((int)((left + width*Math.cos(angle) - height*Math.sin(angle))*compareW),
                (int)((top + width*Math.sin(angle) + height*Math.cos(angle))*compareH));*/

        Point p1 = new Point((int)(left), (int)(top));
        Point p2 = new Point((int)((left + width*Math.cos(angle))), (int)((top + width*Math.sin(angle))));
        Point p3 = new Point((int)((left - height*Math.sin(angle))), (int)((top + height*Math.cos(angle))));
        Point p4 = new Point((int)((left + width*Math.cos(angle) - height*Math.sin(angle))),
                (int)((top + width*Math.sin(angle) + height*Math.cos(angle))));


        //重置
        path.reset();
        path.moveTo(p1.x, p1.y);
        path.lineTo(p2.x, p2.y);
        path.lineTo(p4.x, p4.y);
        path.lineTo(p3.x, p3.y);
        path.lineTo(p1.x, p1.y);
        //图像刷新
        invalidate();
    }
}
